/*
* ============================================================================
* 功能描述 ：串口命令层
* 作者       ：ggls
* 创建日期 ：2024.04.10
* ----------------------------------------------------------------------------
* 更新历史 ：
*	日期 　　- 修改者   : 更新内容描述
*
* ============================================================================
*/

#include "main.h"

static uart_buf_s uart_buf;
static void _uart_data_handle(void)
{
	char  th;
	char  ch;
	if(uart_buf.recv_buf[0] == 0x55)
	{
		if(uart_buf.recv_buf[1] == 0xaa)
		{
			if(uart_buf.recv_len > 7)
			{
				th = uart_buf.recv_buf[6]*256+uart_buf.recv_buf[7];	// 计算传输命令字节数，判断校验位，数据长度是否足够
				uart_buf.cmd_len = th+9;
			
			
				if(uart_buf.recv_len == uart_buf.cmd_len)
				{
				
							
								int i;
								for(i = 0; i < uart_buf.recv_len; i++)
								{
									uart_buf.cmd_buf[i] = uart_buf.recv_buf[i];
								}
								uart_buf.recv_hand_flag = true;
								uart_buf.recv_len = 0;
					
				}
			}
			
		}
	}
	else if (uart_buf.recv_buf[0] != 0x55)
		uart_buf.recv_len = 0;
}

void uart_cmd_rx_cplt_callback(void)
{
	uart_buf.recv_buf[uart_buf.recv_len] = RCREG;
	uart_buf.recv_len++;
	RCIF = 0;
	if (uart_buf.recv_len >22)
		uart_buf.recv_len = 0;
	
	_uart_data_handle();
}



void Zigbee_Cmd_handle(void)
{
	char ch = 0;
	if(uart_buf.recv_hand_flag == true)
	{
		
		uart_buf.recv_hand_flag = false;
		
		Uart_Tx_buf(uart_buf.cmd_buf, uart_buf.cmd_len);
		
			switch(uart_buf.cmd_buf[4])
			{
				case DEVICE_MANAGE:
					Zigbee_Device_Manage(uart_buf.cmd_buf, uart_buf.cmd_len);
					break;
				case NETWORK_MANAGE:
					Zigbee_Network_Manage(uart_buf.cmd_buf, uart_buf.cmd_len);
					break;
				case SERIAL_CONFIG_MANAGE:

					break;
				case OTA_MANAGE:

					break;
				case PRODUCT_MANAGE:

					break;
				default:
					break;

			}
		
	}

}



